//
//  MineShengCityViewController.m
//  shopping
//
//  Created by 马田成 on 2017/9/22.
//  Copyright © 2017年 qianyi. All rights reserved.
//

#import "MineShengCityViewController.h"
#import "MineShengCityTableViewCell.h"
@interface MineShengCityViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;



@end

@implementation MineShengCityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArr = [[NSMutableArray alloc] init];
    [self creationView];
    [self regeditCell];
    [self creationData];
    // Do any additional setup after loading the view.
}
- (void)creationView{
    
    self.tableView = [[UITableView alloc] init];
    self.tableView.frame = CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT - 64);
    self.tableView.tableFooterView = [[UITableView alloc] initWithFrame:CGRectZero];
    
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.separatorStyle = UITableViewCellAccessoryNone;//去掉分割线
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
   
    [self setTitle:@"选择地址"];
    
}
- (void)regeditCell{
    
    
    
    [self.tableView registerNib:[UINib nibWithNibName:@"MineShengCityTableViewCell" bundle:nil] forCellReuseIdentifier:@"MineShengCityTableViewCell"];
    
    
    
}
- (void)creationData{
    
    
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = YES;
    if ([self.type isEqualToString:@"1"]) {
        
        
        [[MNetworkHandler sharedInstance]conURL:adminGoodsgetProvinceList networkType:NetWorkPOST params:nil delegate:nil showHUD:NO successBlock:^(id returnData) {
            
            if ([[returnData objectForKey:@"code"]intValue]==1) {
                
                self.dataArr = [returnData objectForKey:@"data"];
                
                 [self.view addSubview:self.tableView];
                NSLog(@"MMMMMMMMMM%@", self.dataArr);
                
                [self.tableView reloadData];
                
                
                [hud removeFromSuperview];
            }
            
            else {
                
                [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                [hud removeFromSuperview];
                
            }
            
        } failureBlock:^(NSError *error) {
            
            NSLog(@"%@",error);
            
//            [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
            [hud removeFromSuperview];
        }];
    }else if ([self.type isEqualToString:@"2"]){
    
        NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
        [productParams setValue:self.provinceId forKey:@"provinceId"];
        [[MNetworkHandler sharedInstance]conURL:adminGoodsgetgetCityListByProvince networkType:NetWorkPOST params:productParams delegate:nil showHUD:NO successBlock:^(id returnData) {
            
            if ([[returnData objectForKey:@"code"]intValue]==1) {
                
                self.dataArr = [returnData objectForKey:@"data"];
                
                 [self.view addSubview:self.tableView];
                NSLog(@"MMMMMMMMMM%@", self.dataArr);
                
                [self.tableView reloadData];
                
                
                [hud removeFromSuperview];
            }
            
            else {
                
                [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                [hud removeFromSuperview];
                
            }
            
        } failureBlock:^(NSError *error) {
            
            NSLog(@"%@",error);
            
//            [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
            [hud removeFromSuperview];
        }];

        
        
    }else {
    
        NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
        [productParams setValue:self.quID forKey:@"provinceId"];
        
        
        [[MNetworkHandler sharedInstance]conURL:adminGoodsGetDistricts networkType:NetWorkPOST params:productParams delegate:nil showHUD:NO successBlock:^(id returnData) {
            
            if ([[returnData objectForKey:@"code"]intValue]==1) {
                
                self.dataArr = [returnData objectForKey:@"data"];
                
                [self.view addSubview:self.tableView];
                NSLog(@"MMMMMMMMMM%@", self.dataArr);
                
                [self.tableView reloadData];
                
                
                [hud removeFromSuperview];
            }
            
            else {
                
                [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                [hud removeFromSuperview];
                
            }
            
        } failureBlock:^(NSError *error) {
            
            NSLog(@"%@",error);
            
//            [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
            [hud removeFromSuperview];
        }];

    
    }
    
}

#pragma mark - ---------- Section的数量 ----------
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
#pragma mark - ---------- 每个Section的高度 ----------
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}

//section 不随tableView滑动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (scrollView == self.tableView)
        
    {
        
        CGFloat sectionHeaderHeight = 64; //header高度
        
        if (scrollView.contentOffset.y <= sectionHeaderHeight && scrollView.contentOffset.y >= 0) {
            
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
            
        } else if (scrollView.contentOffset.y >= sectionHeaderHeight) {
            
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
            
        }
        
    }
    
    
    
}


#pragma mark - ---------- Cell的数量 ----------
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    
    return self.dataArr.count;
    
}

#pragma mark - ---------- 每个Cell的高度 ----------
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 40;
    
}
#pragma mark - ---------- 每个Cell的内容 ----------
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MineShengCityTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"MineShengCityTableViewCell" forIndexPath:indexPath];
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSMutableDictionary *dic = self.dataArr[indexPath.row];
    cell.diZhiLabel.text = [dic objectForKey:@"areaName"];
    
    return cell;
    
    
}
#pragma mark - ---------- 每个Cell的点击事件 ----------
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *addressID = [self.dataArr[indexPath.row] objectForKey:@"areaId"];
    NSString *address =[self.dataArr[indexPath.row] objectForKey:@"areaName"];
    
    if ([self.type isEqualToString:@"1"]) {
        
        [self.delegate withString:address addressID:addressID type:@"1"];
    }else if ([self.type isEqualToString:@"2"]){
    
        [self.delegate withString:address addressID:addressID type:@"2"];
    }else {
    
         [self.delegate withString:address addressID:addressID type:@"3"];
        
    }
    
    [self.navigationController popViewControllerAnimated:YES];
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
